package com.xs.gis.domain.geojson;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.Serializable;
import java.util.List;

/**
 * @program:
 * @description: GeoFeature 要素描述
 * @author: w
 * @create: 2023-08-17 11:34
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeoJsonFeature implements Serializable {

    /**
     * 类型
     */
    private String type="FeatureCollection";
    /***
     *  features
     */
    private List<GeoFeature> features;

    /***
     * 参考系
     */
    private GeoCrs crs;

    /**
     * 图层名称
     */
    private String layerName;

    /**
     * 总条数
     */
    private long total=0;

    public long getTotal() {
        return  total<=0?features!=null?features.size():0:total;
    }
    /***
     * 获得epsc
     * @return
     */
    @JsonIgnore
    public String getEpsg() {

        if (crs == null || crs.getProperties() == null || crs.getProperties().getName() == null) {
            throw new NullPointerException("参考系数据不存在!");
        }
        return crs.getProperties().getName();

    }

    /**
     * 获得epscCode
     *
     * @return
     */
    @JsonIgnore
    public String getEpsgCode() {

        if (crs == null || crs.getProperties() == null || crs.getProperties().getName() == null) {
            throw new NullPointerException("参考系数据不存在!");
        }

        String name = crs.getProperties().getName();
        try {
            CoordinateReferenceSystem crs = CRS.decode(name);
            return CRS.lookupEpsgCode(crs,false).toString();
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        }


    }

    public String getLayerName() {
        if(StringUtils.isBlank(layerName)){
            return "Temp_1";
        }else{
            return this.layerName;
        }
    }
}
